package com.ton.utils;

import com.kypj.frame.common.DateUtil;
import com.kypj.frame.common.EmptyUtil;

import java.util.Date;
import java.util.List;

public class SqlArgUtil {

    public static Date parseDate(Long time) {
        if (time != null && time > 0) {
            return new Date(time);
        }
        return null;
    }

    public static String like(String key) {
        if (EmptyUtil.isNotEmpty(key)) {
            return "%" + key + "%";
        }
        return null;
    }

    public static Date parseDateBegin(String date) {
        if (EmptyUtil.isEmpty(date)) {
            return null;
        }
        return DateUtil.getDate(date + " 00:00:00", "yyyy-MM-dd HH:mm:ss");
    }

    public static Date parseDateEnd(String date) {
        if (EmptyUtil.isEmpty(date)) {
            return null;
        }
        return DateUtil.getDate(date + " 23:59:59", "yyyy-MM-dd HH:mm:ss");

    }

    public static <T> String sqlIn(List<T> argList, SqlParseBack<T> back) {

        StringBuilder sb = new StringBuilder("(");
        for (T t : argList) {
            sb.append(back.call(t)).append(",");
        }
        if (sb.length() > 2) {
            sb.deleteCharAt(sb.length() - 1);
        }
        sb.append(")");
        return sb.toString();
    }

    public interface SqlParseBack<T> {
        public String call(T t);
    }

    public static String getTable_yyyyMM(String table, Date date) {
        return table + "_" + DateUtil.formatDate(date, "yyyyMM");
    }
}
